# PLAYOFF SCHEDULE SCRIPT
# This script schedules the first and second rounds of playoffs in a season based
# on command line arguments provided. 
#
# Created on December 10, 2009 by Caleb Goff

#!/bin/bash
set +x

# Read in command line arguments for league ID and schedule type
league=$2

# Schedule type should be either
#   1 - 4 teams (first round, for leagues > 4 players)
#   2 - 2 teams (final round, for leagues > 4 players)
#   3 - 2 teams (final round, for leagues <= 4 players)
type=$3
numTeams=0
week=0
num=4

# Select the number of teams to grab based on the type of playoffs
# we are scheduling
if [ $type -eq 1 ] ; then
    num=4
fi

if [ $type -eq 2 ] ; then
    num=4
fi

if [ $type -eq 3 ] ; then
    num = 2
fi

# Grab teams
mysql FBT52009 -p$1 -horion.csl.mtu.edu > teams.txt <<EOFMYSQL
SELECT TEAM_ID FROM TEAMS 
WHERE LEAGUE_ID = '$league' 
ORDER BY PCT DESC, POINTS DESC
LIMIT $num
EOFMYSQL

count=0
declare -a ARRAY

# Load the IDs into an array for scheduling purposes
for line in $(sed '1d' teams.txt)
do  
    ARRAY[($count)]=$line
    ((count++))
done

rm -f teams.txt

# Cycle through the array and find out the latest week
ELEMENTS=${#ARRAY[@]}

# If the schedule type is 1, schedule first round of playoffs
if [ $type -eq 1 ] ; then

    for (( j=0;j<($ELEMENTS-1);j++)); do
        mysql FBT52009 -p$1 -horion.csl.mtu.edu > week.txt <<EOFMYSQL
        SELECT WEEK FROM GAMES WHERE TEAM1 = '${ARRAY[j]}' OR TEAM2 = '${ARRAY[j]}'
        ORDER BY WEEK DESC
        LIMIT 1
EOFMYSQL

        tempWeek=$(sed '1d' week.txt)

        echo TempWeek: $tempWeek

        if [ $tempWeek -gt $week ] ; then
            week=$tempWeek
        fi 

        rm -f week.txt
    done

    curWeek=$(($week+1))

    mysql FBT52009 -p$1 -horion.csl.mtu.edu > week.txt <<EOFMYSQL
    INSERT INTO GAMES (TEAM1, TEAM2, WEEK) VALUES('${ARRAY[0]}', '${ARRAY[3]}', '$curWeek');
    INSERT INTO GAMES (TEAM1, TEAM2, WEEK) VALUES('${ARRAY[1]}', '${ARRAY[2]}', '$curWeek');
EOFMYSQL
fi

# If the schedule type is 2, schedule the final round of playoffs
if [ $type -eq 2 ] ; then
        
    # Schedule last round of playoffs
    for (( j=0;j<($ELEMENTS-1);j++)); do
        mysql FBT52009 -p$1 -horion.csl.mtu.edu > week.txt <<EOFMYSQL
        SELECT WEEK FROM GAMES WHERE TEAM1 = '${ARRAY[j]}' OR TEAM2 = '${ARRAY[j]}'
        ORDER BY WEEK DESC
        LIMIT 1
EOFMYSQL

        tempWeek=$(sed '1d' week.txt)

        echo TempWeek: $tempWeek

        if [ $tempWeek -gt $week ] ; then
            week=$tempWeek
        fi 

        rm -f week.txt
    done

    curWeek=$(($week+1))

    # Find the winners of the previous week
    mysql FBT52009 -p$1 -horion.csl.mtu.edu > winners.txt <<EOFMYSQL
    SELECT CONCAT(TEAM1,'|',TEAM2,'|',TEAM_1_SCORE,'|',TEAM_2_SCORE) AS '|'
    FROM GAMES
    WHERE WEEK = '$week'
EOFMYSQL

    count=0

    declare -a WINNERS
    
    IFS='
    '
    for line in $(sed '1d' winners.txt)
    do
	    team1=$(echo $line | awk -F\| '{print $1}')
        team2=$(echo $line | awk -F\| '{print $2}')
        team1score=$(echo $line | awk -F\| '{print $3}')
        team2score=$(echo $line | awk -F\| '{print $4}')
        if [ $team1score -gt $team2score ] ; then
            WINNERS[(count)]=$team1
        else
            WINNERS[(count)]=$team2
        fi
        ((count++))
    done

    echo ${WINNERS[0]}
    echo ${WINNERS[1]}

    mysql FBT52009 -p$1 -horion.csl.mtu.edu > week.txt <<EOFMYSQL
    INSERT INTO GAMES (TEAM1, TEAM2, WEEK) VALUES('${WINNERS[0]}', '${WINNERS[1]}', '$curWeek');
EOFMYSQL
      
fi

# If the schedule type is 3, schedule final round of playoffs between top two teams
if [ $type -eq 3 ] ; then

    for (( j=0;j<($ELEMENTS-1);j++)); do
        mysql FBT52009 -p$1 -horion.csl.mtu.edu > week.txt <<EOFMYSQL
        SELECT WEEK FROM GAMES WHERE TEAM1 = '${ARRAY[j]}' OR TEAM2 = '${ARRAY[j]}'
        ORDER BY WEEK DESC
        LIMIT 1
EOFMYSQL

        tempWeek=$(sed '1d' week.txt)

        echo TempWeek: $tempWeek

        if [ $tempWeek -gt $week ] ; then
            week=$tempWeek
        fi 

        rm -f week.txt
    done

    curWeek=$(($week+1))

    mysql FBT52009 -p$1 -horion.csl.mtu.edu > week.txt <<EOFMYSQL
    INSERT INTO GAMES (TEAM1, TEAM2, WEEK) VALUES('${ARRAY[0]}', '${ARRAY[3]}', '$curWeek');
    INSERT INTO GAMES (TEAM1, TEAM2, WEEK) VALUES('${ARRAY[1]}', '${ARRAY[2]}', '$curWeek');
EOFMYSQL
fi


